findcomment=re.compile((r'<span>(.*?)</span>'),re.S)
def geturl(url):
    head={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.55"}
    req=urllib.request.Request(url=url,headers=head)
    html=""
    try:
        response=urllib.request.urlopen(req)
        html=response.read().decode("utf-8")
    except url.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    return  html

def getdata():
    file = open(r"C:\Users\小姜\Desktop\fitbit flex2.txt", "a", encoding="utf-8")
    for i in range(1,101):#获取10页
        if i%10==0:
            time.sleep(random.random()+1)
        url="https://www.amazon.com/Fitbit-Flex-Black-Version-Count/product-reviews/B01KH2PV4U/ref=cm_cr_arp_d_paging_btm_next_2?ie=UTF8&reviewerType=all_reviews&pageNumber="+str(i)
        html=geturl(url)
        soup=BeautifulSoup(html,"html.parser")
        for item in soup.find_all('div',class_="a-row a-spacing-small review-data"):
             item=str(item)
             comment=re.findall(findcomment,item)[0]
             file.write(comment)
             file.write("\n")


getdata()ry: